﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.VisionModels;
using thinger.VisitionDAL;

namespace thinger.VisionDemo
{
    public partial class FrmLogin : Form
    {
        List<SysAdmins> AdminList = new List<SysAdmins>();
        List<string> UserList = new List<string>();
        public FrmLogin()
        {
            InitializeComponent();
            this.Load += FrmLogin_Load;
        }
        private void FrmLogin_Load(object sender, EventArgs e)
        {
            InitialUserList();
        }
        private void InitialUserList()
        {

            AdminList = Mainpro.Instance.GetAdminList();

            if (AdminList.Count < 1)
            {
                MessageBox.Show("用户列表为空！", "系统登录");
            }
            else
            {
                foreach (var item in AdminList)
                {

                    UserList.Add(item.LoginName.ToString());
                }
            }
            if (UserList.Count > 0)
            {
                this.cmb_User.DataSource = UserList;
                this.cmb_User.SelectedIndex = 0;
            }
        }
        private void btn_Login_Click(object sender, EventArgs e)
        {
            //数据验证
            if (this.cmb_User.Text.Length == 0)
            {
                MessageBox.Show("请选择用户名！", "登录提示");
                this.cmb_User.Focus();
                return;
            }

            if (this.txt_LoginPwd.Text.Length == 0)
            {
                MessageBox.Show("请填写登录密码！", "登录提示");
                this.txt_LoginPwd.Focus();
                return;
            }

            //封装对象
            SysAdmins objAdmin = new SysAdmins()
            {
                LoginName = this.cmb_User.Text.Trim(),
                //给密码做加密
                LoginPwd = Register.Encrypt(this.txt_LoginPwd.Text.Trim())
            };

            //登录验证
            if (AdminCheck(objAdmin))
            {
                //设置DialResult
                this.DialogResult = DialogResult.OK;
                //MessageBox.Show("登陆成功！", "登录提示");
                //保存用户信息
                // CommonMethods.objAdmin = objAdmin;
                this.Close();
            }
            else
            {
                MessageBox.Show("用户名或密码错误！", "登录提示");
            }
        }


        #region 快捷登录
        private void txt_LoginPwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btn_Login_Click(null, null);
            }
        }

        private void cmb_User_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btn_Login_Click(null, null);
            }
        }
        #endregion

        private bool AdminCheck(SysAdmins objAdmin)
        {
            if (AdminList == null)
            {
                return false;
            }

            foreach (var item in AdminList)
            {
                if (item.LoginName == objAdmin.LoginName && item.Pwd == objAdmin.LoginPwd)
                {
                    GlobalVar.Instance.sysAdmins = item;
                    return true;
                }

            }

            return false;
        }
    }
}
